package tools;

public class Time {
	
	public static final int MAX_SECONDS = 59;
	public static final int MAX_MINUTES = 59;
	public static final int MAX_HOURS = 23;
	public static final int MAX_DAYS = 30;
	public static final int MAX_MONTHS = 11;
	
	private int seconds = 0;
	private int minutes = 0;
	private int hours = 0;
	private int days = 0;
	private int months = 0;
	private int years = 0;

	public Time (int seconds, int minutes, int hours, int days, int months, int years) {
		this.seconds = seconds;
		this.minutes = minutes;
		this.hours = hours;
		this.days = days;
		this.months = months;
		this.years = years;
	}
	
	public Time (int seconds, int minutes, int hours, int days, int months) {
		this(seconds, minutes, hours, days, months, 0);
	}
	
	public Time (int seconds, int minutes, int hours, int days) {
		this(seconds, minutes, hours, days, 0, 0);
	}
	
	public Time (int seconds, int minutes, int hours) {
		this(seconds, minutes, hours, 0, 0, 0);
	}
	
	public Time (int seconds, int minutes) {
		this(seconds, minutes, 0, 0, 0, 0);
	}
	
	public Time (int seconds) {
		this(seconds, 0, 0, 0, 0, 0);
	}
	
	public Time() {
		this(0, 0, 0, 0, 0, 0);
	}

	public void increment(int seconds) {
		this.seconds += seconds;
		while (this.seconds > Time.MAX_SECONDS) {
			this.seconds -= Time.MAX_SECONDS + 1;
			this.minutes += 1;
		}
		while (this.minutes > Time.MAX_MINUTES) {
			this.minutes -= Time.MAX_MINUTES + 1;
			this.hours += 1;
		}
		while (this.hours > Time.MAX_HOURS) {
			this.hours -= Time.MAX_HOURS + 1;
			this.days += 1;
		}
		while (this.days > Time.MAX_DAYS) {
			this.days -= Time.MAX_DAYS + 1;
			this.months += 1;
		}
		while (this.months > Time.MAX_MONTHS) {
			this.months -= Time.MAX_MONTHS + 1;
			this.years += 1;
		}
	}
	
	public void add(Time toAdd) {
		this.seconds += toAdd.seconds;
		while (this.seconds > Time.MAX_SECONDS) {
			this.seconds -= Time.MAX_SECONDS + 1;
			this.minutes += 1;
		}
		this.minutes += toAdd.minutes;
		while (this.minutes > Time.MAX_MINUTES) {
			this.minutes -= Time.MAX_MINUTES + 1;
			this.hours += 1;
		}
		this.hours += toAdd.hours;
		while (this.hours > Time.MAX_HOURS) {
			this.hours -= Time.MAX_HOURS + 1;
			this.days += 1;
		}
		this.days += toAdd.days;
		while (this.days > Time.MAX_DAYS) {
			this.days -= Time.MAX_DAYS + 1;
			this.months += 1;
		}
		this.months += toAdd.months;
		while (this.months > Time.MAX_MONTHS) {
			this.months -= Time.MAX_MONTHS + 1;
			this.years += 1;
		}
		this.years += toAdd.years;
	}
	
	public int getSeconds() {
		return seconds;
	}

	public int getMinutes() {
		return minutes;
	}

	public int getHours() {
		return hours;
	}

	public int getDays() {
		return days;
	}

	public int getMonths() {
		return months;
	}

	public int getYears() {
		return years;
	}

	@Override
	public String toString() {
		StringBuilder strBuilder = new StringBuilder();
		if (this.years > 0) {
			strBuilder.append(this.years);
			strBuilder.append((this.years != 1 ? " years " : " year "));
		}
		if (this.months > 0) {
			strBuilder.append(this.months);
			strBuilder.append((this.months != 1 ? " months " : " month "));
		}
		if (this.days > 0) {
			strBuilder.append(this.days);
			strBuilder.append((this.days != 1 ? " days " : " day "));
		}
		if (this.hours > 0) {
			strBuilder.append(this.hours);
			strBuilder.append((this.hours != 1 ? " hours " : " hour "));
		}
		if (this.minutes > 0) {
			strBuilder.append(this.minutes);
			strBuilder.append((this.minutes != 1 ? " minutes " : " minute "));
		}
		strBuilder.append(this.seconds);
		strBuilder.append((this.seconds != 1 ? " seconds " : " second "));
		return strBuilder.toString();
	}
}
